<template>
    <van-button
      v-if="isFollowed"
      class="follow-btn"
      round
      :loading="loading"
      size="small"
      @click="onFollow"
      >已关注</van-button
    >
    <van-button
      v-else
      class="follow-btn"
      type="info"
      color="#3296fa"
      round
      :loading="loading"
      size="small"
      icon="plus"
      @click="onFollow"
      >关注</van-button
    >
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'
export default {
  name: 'FollowUser',
  model: {
    prop: 'isFollowed',
    event: 'update-is-followed'
  },
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    userId: {
      type: [Number, String, Object],
      required: true
    }
  },
  components: {},
  data () {
    return {
      loading: false
    }
  },
  created () {},
  mounted () {},
  methods: {
    async onFollow () {
      this.loading = true
      try {
        if (this.isFollowed) {
        // 取消关注
          await addFollow(this.userId)
        } else {
          await deleteFollow(this.userId)
        }
        this.$emit('update-is-followed', !this.isFollowed)
      } catch (e) {
        if (e.response && e.response.status === 400) {
          return this.$toast('不能关注自己')
        }
        this.$toast('服务器错误')
      }
      this.loading = false
    }
  },
  watch: {},
  computed: {}
}
</script>

<style lang="less" scoped>
</style>
